package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Food;
import pojo.FoodCate;
import service.ICateService;
import service.IFoodService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/food")
public class FoodController {
    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    private IFoodService foodService;

    @Resource
    private ICateService cateService;

    /**
     * 初始化餐品管理页面
     * @param rid   餐厅id
     * @return  类别列表,餐品列表
     */
    @RequestMapping("/init")
    @ResponseBody
    public Map<String, Object> initFoodManage(@RequestParam Integer rid) {
        responseMsg.clear();
        List<Food> list = foodService.listFood(rid);
        List<FoodCate> cateList = cateService.listCate(rid);
       for (Food food : list){
         for (FoodCate foodCate:cateList){
             if (food.getCateId()==foodCate.getId()){
                 food.setCateName(foodCate.getName());
             }
         }
       }
        if (list.isEmpty()&&cateList.isEmpty()) {
            responseMsg.put("success", false);
            responseMsg.put("message", "initialize food manage failure");
        } else {
            responseMsg.put("success", true);
            responseMsg.put("message", "initialize food manage success");
            responseMsg.put("food_list", list);
            responseMsg.put("cate_list", cateList);
        }
        return responseMsg;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> foodList(@RequestParam Integer rid) {
        responseMsg.clear();
        List<Food> list = foodService.listFood(rid);
        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "list food success");
            responseMsg.put("food_list", list);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "list food failure");
        }
        return responseMsg;
    }

    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object> searchFood(@RequestBody Food food) {
        responseMsg.clear();
        System.out.println("restid:" + food.getRestaurantId());
        System.out.println("cateid:" + food.getCateId());
        System.out.println("title" + food.getTitle());
        List<Food> list = foodService.searchFood(food);

        for (Food f :
                list) {
            System.out.println(f);
        }
        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "search food success");
            responseMsg.put("food_list", list);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "search food failure");
        }
        return responseMsg;
    }

    @RequestMapping("/newfood")
    @ResponseBody
    public Map<String, Object> newFood(@RequestBody Food food) {
        responseMsg.clear();
        int result = foodService.newFood(food);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "add new food success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "add new food failure");
        }
        return responseMsg;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateFood(@RequestBody Food food) {
        responseMsg.clear();
        int result = foodService.updateFood(food);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "update food success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "update food failure");
        }
        return responseMsg;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteFood(@RequestParam Integer id) {
        responseMsg.clear();
        int result = foodService.removeFood(id);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "delete food success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "delete food failure");
        }
        return responseMsg;
    }
}
